import store from '@/store'

/**权限指令
 * 用法  v-permitBtn="`1004, 1006`"  多个ID一起判断
 *   v-permitBtn="`1004`"  判断一个ID
 *  v-permitBtn="[`1004`, '文案']"  用文案替换按钮
 *  v-permitBtn="[`1004,3423,5234,23121`, '文案']"  多个ID替换文案
 * **/
export const permitBtn = function() {
  return {
    // 指令绑定元素挂载前
    mounted(el, binding, vnode) {
      let userInfo = {}
      if (sessionStorage.getItem('userInfo')) {
        userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      }
      // const butPremissions = permStore().butPremissions;
      const butPremissions = store.state.permission.butPremissions;
  
      let bindingValue = binding.value
      let text = ''
      if (Array.isArray(binding.value)) {
        bindingValue = binding.value[0]
        text = binding.value[1]
      }
      const bindingList = bindingValue.split(',');
  
      let nodeTextBool = butPremissions.includes(bindingList[0])
      if (bindingList.length > 1) {
        // 如果传进来的ID 为数组 只要有一个就不做删除
        nodeTextBool = bindingList.some(e => {
          return butPremissions.includes(e)
        })
      }
      if (butPremissions.length && !nodeTextBool && !userInfo.isAdmin) {
        if (text) {
          var node1 = document.createTextNode(text);  //创建文本节点
          el.parentNode.appendChild(node1)
        }
        el.parentNode.removeChild(el);
      }
    }
  }
}